From aae6c90fdf9b6c1af1b0294d812910dfe135ec6f Mon Sep 17 00:00:00 2001 From: Tor Lillqvist Date: Thu, 20 Mar 2008 03:00:45 +0000 Subject: [PATCH] Bug 314084 - GTK+ dialogs should not be placed partially offscreen 2008-03-20 Tor Lillqvist Bug 314084 - GTK+ dialogs should not be placed partially offscreen * gtk/gtkwindow.c (clamp): New function. Clamps a window position in one dimension, or centered in case it doesn't fit. (clamp_window_to_rectangle): Simplify. Call clamp() for x and y dimensions. svn path=/trunk/; revision=19907 --- ChangeLog | 9 +++++++++ gtk/gtkwindow.c | 41 +++++++++++++++++++++++++++-------------- 2 files changed, 36 insertions(+), 14 deletions(-) diff --git a/ChangeLog b/ChangeLog index 064eeed5c3..8ee0c5c110 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,12 @@ +2008-03-20 Tor Lillqvist + + Bug 314084 - GTK+ dialogs should not be placed partially offscreen + + * gtk/gtkwindow.c (clamp): New function. Clamps a window position + in one dimension, or centered in case it doesn't fit. + (clamp_window_to_rectangle): Simplify. Call clamp() for x and y + dimensions. + 2008-03-18 Tor Lillqvist Bug 523225 - modules/input/im*.c: MODULE_ENTRY macros make illegal code diff --git a/gtk/gtkwindow.c b/gtk/gtkwindow.c index 04dc992f3b..27412221cb 100644 --- a/gtk/gtkwindow.c +++ b/gtk/gtkwindow.c @@ -5516,6 +5516,21 @@ center_window_on_monitor (GtkWindow *window, *y = monitor.y; } +static void +clamp (gint *base, + gint extent, + gint clamp_base, + gint clamp_extent) +{ + if (extent > clamp_extent) + /* Center */ + *base = clamp_base + clamp_extent/2 - extent/2; + else if (*base < clamp_base) + *base = clamp_base; + else if (*base + extent > clamp_base + clamp_extent) + *base = clamp_base + clamp_extent - extent; +} + static void clamp_window_to_rectangle (gint *x, gint *y, @@ -5523,21 +5538,19 @@ clamp_window_to_rectangle (gint *x, gint h, const GdkRectangle *rect) { - gint outside_w, outside_h; - - outside_w = (*x + w) - (rect->x + rect->width); - if (outside_w > 0) - *x -= outside_w; - - outside_h = (*y + h) - (rect->y + rect->height); - if (outside_h > 0) - *y -= outside_h; +#ifdef DEBUGGING_OUTPUT + g_print ("%s: %+d%+d %dx%d: %+d%+d: %dx%d", __FUNCTION__, rect->x, rect->y, rect->width, rect->height, *x, *y, w, h); +#endif - /* if larger than the screen, center on the screen. */ - if (*x < rect->x) - *x += (rect->x - *x) / 2; - if (*y < rect->y) - *y += (rect->y - *y) / 2; + /* If it is too large, center it. If it fits on the monitor but is + * partially outside, move it to the closest edge. Do this + * separately in x and y directions. + */ + clamp (x, w, rect->x, rect->width); + clamp (y, h, rect->y, rect->height); +#ifdef DEBUGGING_OUTPUT + g_print (" ==> %+d%+d: %dx%d\n", *x, *y, w, h); +#endif } -- 2.30.2